//index.js
//获取应用
Page({
  
  //事件处理函数
  data: { 
    plateNumberList:null,
    existCarNos:null,
    path: null,
    extData: null,
    addCar: true,
    isAddCar: false,
    carNo: "",
    path: "",
    extData: ""
  },
  onShow: function () {
    wx.redirectTo({
      url: '../gateStaticPay/gateStaticPay'
    })
    this.setData({
      extData: {
        recordId: 455498
      }
    });
    var openid = wx.getStorageSync("openid")
    var reqUnionid = wx.getStorageSync("reqUnionid")
    var obj
    var self = this
    if (reqUnionid && openid){
      wx.request({
        url: 'https://kuaifu.boshukeji.com/api/userInfo/weChat/get',
        data: {
          appid: "wx0c6ddc854ee29b49",
          openid: openid,
          unionid: reqUnionid,
          thirdPlatformType: 2
        },
        success: res => {
          console.log(res)
          if (res.data.result) {
            self.queryUserStatus(openid);
          }
        }
      })
    }else{
      wx.login({
        success: res => {
          console.log(res);
          if (res.code) {
            console.log(res);
            wx.request({
              url: "https://kuaifu.boshukeji.com/api/userInfo/weChat/getOpenid",
              data: {
                code: res.code,
              },
              success: res => {
                console.log(res);
                if (res.data) {
                  console.log(res.data);
                  var unionid = res.data.unionid
                  var openid = res.data.openid
                  
                  // TODO post小程序appid openid userid到服务器上保存 
                  wx.request({
                    url: 'https://kuaifu.boshukeji.com/api/userInfo/weChat/get',
                    data: {
                      appid: "wx0c6ddc854ee29b49",
                      openid: openid,
                      unionid: unionid,
                      thirdPlatformType: 2
                    },
                    success: res => {
                      wx.setStorageSync("openid", openid)
                      wx.setStorageSync("reqUnionid", unionid)
                    
                      if (res.data.result) {
                        self.queryUserStatus(openid);
                      }
                    }
                  })
                }
              }
            })
          }
        }
      })
    }
  },
  queryUserStatus: function (openid) {
    var self = this;
    wx.showToast({
      icon: 'loading',
      duration: 1000
    }),
    wx.request({
      url: "https://kuaifu.boshukeji.com/api/userInfo/queryUserStatus",
      data: {
        openid: openid
      },
      success: res => {
        if (res.data.result) {
          console.log(res.data)
          var plateNumberList = res.data.data.plateNumberList
          if (res.data.data.existCarNos){
            console.log(res.data.data.existCarNos)
            this.setData({
              existCarNos: res.data.data.existCarNos
            });
          }
          if (res.data.data.userState != "OVERDUE") {
            this.setData({
              addCar: true
            });
          }
          if (res.data.data.existCarNos.length != 0) {
            plateNumberList = plateNumberList.split(";");
            this.setData({
              plateNumberList: plateNumberList
            });
          } else {
            if (res.data.data.existCarNos != "OVERDUE"){

              wx.navigateTo({
                url: '../keyboard/keyboard'
              })
            }else{
              this.setData({
                addCar: false
              });
            }
          }
        }
      }
    })
  },
  addCar: function(){
    wx.navigateTo({
      url: '../keyboard/keyboard'
    })
  },
  delCarNo: function(event){
    var self = this;
    console.log(event)
    wx.showModal({
      title: '提示',
      content: '确定删除？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: 'https://kuaifu.boshukeji.com/api/userCarApi/delCar',
            data: {
              openid: wx.getStorageSync("openid"),
              plateNumber: event.currentTarget.dataset.car
            },
            success: res => {
              console.log(res.data)
              if (res.data.success) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 1000
                })
                var existCarNos = self.data.existCarNos;
                existCarNos.splice(event.currentTarget.dataset.index, 1)
                console.log(existCarNos)
                self.setData({
                  existCarNos: existCarNos
                })
              } else {
                wx.showModal({
                  title: '删除提示',
                  content: "对不起，删除之前请先关闭此车牌的无感支付功能！",
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  showModel(e) {
    wx.showToast({
      icon: 'loading',
      duration: 1000
    }),
      wx.request({
        url: "https://kuaifu.boshukeji.com/api/userInfo/queryUserStatus",
        data: {
          openid: wx.getStorageSync("openid"),
          plateNumber: e.currentTarget.dataset.car
        },
        success: res => {
          if (res.data.result) {
            console.log(res.data.data)
            var plateNumberList = res.data.data.plateNumberList
            if (plateNumberList.length != 0) {
              plateNumberList = plateNumberList.split(",");
            }
            
            var extData = {
              appid: res.data.data.appId,
              mch_id: res.data.data.mchId,
              nonce_str: res.data.data.nonceStr,
              sign: res.data.data.sign,
              sign_type: res.data.data.signType,
              sub_appid: res.data.data.subAppId,
              sub_mch_id: res.data.data.subMchId,
              sub_openid: res.data.data.subOpenId,
              plate_number: res.data.data.plateNumber,
              trade_scene: "PARKING"
            }

            console.log(res.data.data.existCarNos);
            console.log(res.data.data.path);
            console.log(res.data.data.extData);
            console.log(extData);
            this.setData({
              isAddCar: true,
              plateNumberList: plateNumberList,
              extData: extData,
              path: res.data.data.path,
              carNo: e.currentTarget.dataset.car
            });
          }
        }
      })
  },
  nono: function () {
    var self = this
    self.setData({
      isAddCar: false,
    });
  }
})
